package zkk.com.mengqu.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

import java.util.ArrayList;
import java.util.List;

import zkk.com.mengqu.App;
import zkk.com.mengqu.R;

/**
 * 首页面
 */
public class HomeFragment extends Fragment implements View.OnClickListener{
    private View view;
    private Context context;
    private List<Fragment> mFragmentList = new ArrayList<>();//首页4个fragment
    private TabLayout tableLayout;//顶部导航栏
    private ViewPager viewPager;
    private ApnFragmentAdapter mAdapter;

    private LinearLayout llLocation;
    private TextView location;
    private ImageView search;

    private LocationClient mLocationClient;//定位SDK的核心类

    private RelativeLayout title;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home, container,false);
        context = getContext();
        initView();
        return view;
    }

    //控件初始化
    public void initView(){
        llLocation = (LinearLayout)view.findViewById(R.id.ll_location);
        location = (TextView) view.findViewById(R.id.tx_location);
        search = (ImageView)view.findViewById(R.id.iv_search);
        title = (RelativeLayout)view.findViewById(R.id.title);

        tableLayout = (TabLayout)view.findViewById(R.id.tableLayout);
        viewPager = (ViewPager)view.findViewById(R.id.viewPager);

        mFragmentList.add(new NearByKitchenFragment());//附近厨房
        mFragmentList.add(new NearByKitchenFragment());//星级厨房
        mFragmentList.add(new NearByKitchenFragment());//美女厨房
        mFragmentList.add(new NearByKitchenFragment());//妈妈厨房

        mAdapter = new ApnFragmentAdapter(getFragmentManager());
        viewPager.setAdapter(mAdapter);
        tableLayout.setupWithViewPager(viewPager);
        tableLayout.setTabMode(TabLayout.MODE_FIXED);

        llLocation.setOnClickListener(this);
        search.setOnClickListener(this);

        //百度地图
        mLocationClient = App.app().mLocationClient;
        mLocationClient.registerLocationListener(App.app().mMyLocationListener);

    }

    @Override
    public void onResume() {
        super.onResume();
        App.app().location = location;

        App.app().title = title;

        InitLocation();//初始化
        mLocationClient.start();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_location:
//                InitLocation();//初始化
//                mLocationClient.start();
                break;
            case R.id.iv_search:
                Toast.makeText(context , "搜索", Toast.LENGTH_LONG).show();
                break;
        }
    }

    //百度地图定位初始化
    private void InitLocation() {
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//设置高精度定位定位模式
        option.setCoorType("bd09ll");//设置百度经纬度坐标系格式
        option.setScanSpan(10000);//设置发起定位请求的间隔时间为1000ms
        option.setIsNeedAddress(true);//反编译获得具体位置，只有网络定位才可以
        option.setIsNeedLocationDescribe(true);
        mLocationClient.setLocOption(option);
    }

    private class ApnFragmentAdapter extends FragmentPagerAdapter {
        CharSequence[] TITLE = new CharSequence[]{"附近厨房", "星级厨房", "美女厨房", "妈妈厨房"};

        public ApnFragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return TITLE.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLE[position];
        }
    }
}
